package com.maven.booksystem.config;


import com.maven.booksystem.model.ResultInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

// 统一 异常处理

@ResponseBody        // 告诉 Spring 这个 返回的是数据不是页面
@ControllerAdvice
public class ErrorAdvice {

    @ExceptionHandler
    public Object handler(Exception ex) {
        return ResultInfo.fail(ex.getMessage());
    }

    @ExceptionHandler
    public Object handler(NullPointerException e) {
        return ResultInfo.fail("发⽣NullPointerException:"+e.getMessage());
    }

    @ExceptionHandler
    public Object handler(ArithmeticException e) {
        return ResultInfo.fail("发⽣ArithmeticException:"+e.getMessage());
    }
}
